<template>
  <div class="editTask" id="editTask">
    <div class="task-paper">
      <Form class="task-form" ref="form" :model="FormData" :label-width="80">
      <!-- 试卷名 -->
        <h1 class="task-title">
          {{FormData.title}}
          {{'（'+FormData.totalScore+'分满）'}}</h1>
        <p class="task-score">您目前的得分是：<span>{{parseInt(FormData.studentScore)}}</span></p>
        <div class="task-h2" v-if="FormData.singleData.length">
          <h2>{{capitalFigures[
              FormData.singleData.length?0:-1
            ]}}. 单项选择{{
            '('+
            '共'+FormData.singleData.length+'题，共'+getSingleScoreADD+
            '分)'
            }}
          </h2>
        </div>
        <div id="single-choice" class="task-single-choice2" >
          <singleChoice :FormData="FormData" @getSingleAnswer="getSingleAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.multipleData.length">
          <h2>{{capitalFigures[
              FormData.singleData.length?1:0
            ]}}. 多项选择
            <span v-if="FormData.multipleData.length">
            {{'(每题'+FormData.multipleData[0].score+
            '分，'+'共'+FormData.multipleData.length+'题，' +
            '共'+FormData.multipleData[0].score*FormData.multipleData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="multiple-choice" class="task-multiple-choice" >
          <multipleChoice :FormData="FormData" @getMultipleAnswer="getMultipleAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.judgeData.length">
          <h2>{{capitalFigures[
              (FormData.singleData.length?1:0) + (FormData.multipleData.length?1:0)
            ]}}. 判断题
            <span v-if="FormData.judgeData.length">
            {{'(每题'+FormData.judgeData[0].score+
            '分，'+'共'+FormData.judgeData.length+'题，' +
            '共'+FormData.judgeData[0].score*FormData.judgeData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="judge" class="task-judge">
          <judge :FormData="FormData" @getJudgeAnswer="getJudgeAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.programmingData.length">
          <h2>{{capitalFigures[
              (FormData.judgeData.length?1:0) + (FormData.singleData.length?1:0) + (FormData.multipleData.length?1:0)
            ]}}. 编程题
            <span v-if="FormData.programmingData.length">
            {{'(每题'+FormData.programmingData[0].score+
            '分，'+'共'+FormData.programmingData.length+'题，' +
            '共'+FormData.programmingData[0].score*FormData.programmingData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="program" class="task-judge">
          <programming :FormData="FormData" :key="time"/>
        </div>
        <div></div>
      </Form>
    </div>
    <toolBox :FormData="FormData" :key="time"/>
  </div>
</template>

<script>
import {Paper} from './questionBank/paper.js'
import singleChoice from './questionBank/singleChoice.vue'
import multipleChoice from './questionBank/multipleChoice.vue'
import judge from './questionBank/judge.vue'
import programming from './questionBank/programming.vue'
import toolBox from './homeworkToolBox.vue'
export default {
  name: 'Homework',
  components: {singleChoice, multipleChoice, judge, programming, toolBox},
  props: ['taskData'],
  data () {
    return {
      s: 0,
      m: 0,
      j: 0,
      p: 0,
      time: new Date().getTime(),
      paper: '',
      getSingleAnswer: {},
      getMultipleAnswer: {},
      getJudgeAnswer: {},
      // 选择题选项
      choiceList: ['A', 'B', 'C', 'D'],
      capitalFigures: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三'],
      // 试卷题型
      FormData: {}
    }
  },
  async created () {
    this.paper = new Paper()
    this.FormData = {
      sectionId: this.taskData.sectionId,
      homework_id: this.taskData.paper.id,
      title: this.taskData.paper.title,
      totalScore: 0,
      singleData: [],
      multipleData: [],
      judgeData: [],
      programmingData: [],
      studentScore: 0
    }
    await this.taskData.paperDetail.forEach(e => {
      if (e.selection_problem_type === 'single') {
        if (Array.isArray(e.answer)) {
          e.answer = e.answer[0]
        }
        this.FormData.singleData.push(e)
      } else if (e.selection_problem_type === 'multiple') {
        this.FormData.multipleData.push(JSON.parse(JSON.stringify(e)))
      } else if (e.selection_problem_type === 'judge') {
        if (Array.isArray(e.answer)) {
          e.answer = e.answer[0]
        }
        this.FormData.judgeData.push(JSON.parse(JSON.stringify(e)))
      }
    })
    await this.taskData.paperDetailProblems.forEach(e => {
      this.FormData.programmingData.push(e)
    })
    // 加载作业分数
    if (this.FormData.singleData[0]) {
      await this.FormData.singleData.forEach(_ => {
        this.s += _.score
      })
    }
    if (this.FormData.multipleData[0]) {
      await this.FormData.multipleData.forEach(_ => {
        this.m += _.score
      })
    }
    if (this.FormData.judgeData[0]) {
      await this.FormData.judgeData.forEach(_ => {
        this.j += _.score
      })
    }
    if (this.FormData.programmingData[0]) {
      await this.FormData.programmingData.forEach(_ => {
        this.p += _.score
      })
    }
    this.FormData.totalScore = await (this.s + this.m + this.j + this.p)
    this.FormData.required = true
  },
  methods: {
  },
  computed: {
    getSingleScoreADD: function () {
      let add = 0
      this.FormData.singleData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getMultipleScoreADD: function () {
      let add = 0
      this.FormData.multipleData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getJudgeScoreADD: function () {
      let add = 0
      this.FormData.judgeData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getProgrammingScoreADD: function () {
      let add = 0
      this.FormData.programmingData.forEach(item => {
        add += Number(item.score)
      })
      return add
    }
  }
}
</script>

<style lang="less">
.task-h2{
  width: 95%;
  height: 50px;
  text-align: left;
  margin:0 30px;
}
.el-form-item__error {
  left: 26%;
}
.el-form-item__content {
  margin-left: 0 !important;
}
.ivu-form-item-content {
  margin-left: 0 !important;
}
@import url('./Homework.less');
.paperBottom {
  height: 30px;
}
</style>
